<template>
  <div class="page-content">
    <table-bar :showTop="false" :columns="columns" @changeColumn="changeColumn">
      <div slot="top">
        <el-form :model="searchForm" label-width="100px" ref="searchForm">
          <form-input
            label="搜索"
            class="el-ft right"
            style="width:300px"
            @input="change($event)"
            v-model="searchForm.keyword"
            prop="keyword"
            placeholder="客户名称/账号名"
            @keyup.enter.native="submitForm('searchForm')"
          ></form-input>
          <el-date-picker
            v-model="binddate"
            type="daterange"
            align="center"
            unlink-panels
            range-separator="至"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            class="el-ft right"
          >
          </el-date-picker>
          <el-row class="el-ft">
            <el-button type="primary" @click="submitForm('searchForm')"
              >搜索</el-button
            >
            <el-button @click="resetForm('searchForm')">重置</el-button>
          </el-row>
        </el-form>
      </div>
    </table-bar>
    <div class="order-table">
      <k-table
        :data="tableData"
        v-loading="loading"
        ref="loginlogtable"
        @sortHandler="sort($event)"
        :page="setpage"
        :total="setpage.total"
        @changePage="gotopage($event)"
        @changeSize="gotosize($event)"
      >
        <el-table-column type="index" width="50" fixed="left">
        </el-table-column>
        <div v-for="(item, index) in tableHeader" :key="index">
          <el-table-column
            :prop="item.name"
            :label="item.title"
            show-overflow-tooltip
            sortable="custom"
            :width="
              item.name === 'writedate'
                ? '180'
                : item.name === 'ddpaper'
                ? '200'
                : item.name === 'guestname'
                ? '200'
                : ''
            "
          >
          </el-table-column>
        </div>
      </k-table>
    </div>
  </div>
</template>

<script>
import { LoginDslogApi } from "@/api/userApi";
export default {
  inject: ["reload"],
  data() {
    return {
      tableData: [],
      tableHeader: [],
      tableBody: [],
      columns: [],
      binddate: [],
      gridDataHeader: [],
      loading: true,
      title: "",
      gridData: [],
      showEmptyTips: true,
      searchForm: {
        keyword: ""
      },
      setpage: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }
    };
  },
  mounted() {
    this.LoginDslogData("", "", 1, this.binddate[0], this.binddate[1]);
  },
  methods: {
    LoginDslogData(prop, order, pageindex, begintime, endtime) {
      let userdata = JSON.stringify(this.$store.state.user.user.info);
      userdata = JSON.parse(userdata);
      let guestcode = userdata.reguserno;
      let gscode = userdata.gscode;
      let username = userdata.userName;
      let userid = userdata.id;
      let logintype = userdata.logintype;
      let posttype = userdata.posttype;
      let sort = "";
      if (order != undefined && order.length > 0 && order != null) {
        if (order == "ascending") {
          sort = prop + " asc ";
        } else if (order == "descending") {
          sort = prop + " desc ";
        }
      }
      LoginDslogApi({
        guestcode: guestcode,
        gscode: gscode,
        sort: sort,
        pagesize: this.setpage.pageSize,
        pageindex: pageindex,
        keyword: this.searchForm.keyword,
        begintime: begintime,
        endtime: endtime,
        username: username,
        userid: userid,
        menuename: "LoginLog",
        menucname: "登录日志",
        logintype: logintype,
        posttype: posttype,
        op_type: "查询"
      }).then(res => {
        this.loading = false;
        this.tableHeader = [];
        this.tableData = [];
        this.setpage.total = res.count;
        res.filesName.forEach(element => {
          if (element.hidden === false) {
            this.tableHeader.push({ title: element.title, name: element.name });
            this.columns.push({ name: element.title, show: true });
          }
        });
        res.data.forEach(element => {
          this.tableData.push({
            guestname: element.guestname,
            logintype: element.logintype,
            loginname: element.loginname,
            openid: element.openid,
            logintime: element.logintime,
            usertype: element.usertype,
            iparea: element.iparea,
            loginip: element.loginip,
            systype: element.systype
          });
        });
      });
    },
    sort(val) {
      this.LoginDslogData(
        val.prop,
        val.order,
        this.binddate[0],
        this.binddate[1]
      );
    },
    gotopage(val) {
      this.LoginDslogData("", "", val.page, this.binddate[0], this.binddate[1]);
    },
    clear() {
      this.gridDataHeader = [];
      this.gridData = [];
    },
    refresh() {
      this.reload();
    },
    change(e) {
      this.$forceUpdate();
    },
    changeColumn(columns) {
      this.columns = columns;
      this.doLayout();
    },
    doLayout() {
      this.$nextTick(() => {
        this.$refs.loginlog.doLayout();
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.LoginDslogData(
            "",
            "",
            this.page,
            this.binddate[0],
            this.binddate[1]
          );
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      var aData = new Date();
      this.value =
        aData.getFullYear() +
        "-" +
        (aData.getMonth() + 1) +
        "-" +
        aData.getDate();
      this.binddate = [];
      this.binddate[0] = this.value;
      this.binddate[1] = this.value;
      this.LoginDslogData("", "", 1, this.value, this.value);
    }
  }
};
</script>

<style lang="scss">
.page-content {
  width: 100%;
  height: 100%;
}
.el-ft {
  float: left;
}
.right {
  margin-right: 10px;
}
.order-table-expand {
  font-size: 0;
}
.order-table-expand label {
  width: 90px;
  color: #2d8cf0;
}
.order-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.order-table {
  height: 100% !important;

  // 分页组件
  ::v-deep .el-pagination {
    margin: 20px 0 0 -5px;
    padding-left: 0;
  }

  // 滚动条
  ::v-deep ::-webkit-scrollbar {
    height: 6px !important;
    display: block !important;
  }

  .empty {
    i {
      color: #d7d6d6;
      font-size: 40px;
    }

    p {
      color: #999999;
      font-size: 13px;
      line-height: 30px;
      margin-top: -10px;
    }
  }
}
.hide-scroll-bar {
  ::v-deep ::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }
}
@media only screen and (max-width: $device-notebook) {
}

@media only screen and (max-width: $device-ipad) {
  // .page-content{
  //   display: none;
  // }
}
</style>
